home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4873 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  58 lines

  1. Path: ipa.bbn.com!user
  2. From: rshapiro@bbn.com (R Shapiro)
  3. Newsgroups: comp.sys.mac.programmer.codewarrior,comp.lang.c++
  4. Subject: templatized pointers to member functions (in CW8)
  5. Date: Thu, 01 Feb 1996 08:33:07 -0500
  6. Organization: Bolt Beranek & Newman
  7. Message-ID: <rshapiro-0102960833080001@ipa.bbn.com>
  8. NNTP-Posting-Host: ipa.bbn.com
  9.  
  10. I need to reference a pointer to a member function within a template,
  11. where the class of the function is determined by the template parameter.
  12.  
  13. Consider:
  14.  
  15. #include <iostream.h>
  16.  
  17. class ATest {
  18.    typedef void (ATest::*ATestObjectMemberFunction)(void);
  19.    public:
  20.       ATest() {}
  21.       void junk () { cout << "Junk" << endl << flush;}
  22.       ATestObjectMemberFunction func () { return &ATest::junk; }
  23. };
  24.  
  25. template <class OBJECT>
  26. class Test {
  27.    // (a) This is what I want but it won't compile in CodeWarrior 8:
  28.    //typedef void (OBJECT::*ObjectMemberFunction)(void); 
  29.  
  30.    // (b) This works but isn't what I want:
  31.    typedef void (ATest::*ObjectMemberFunction)(void); 
  32.    
  33.    private:
  34.       OBJECT& object;
  35.       
  36.    public: 
  37.       Test(OBJECT& object) : object(object) {}
  38.       void run(ObjectMemberFunction func) { (object.*func)(); }
  39. };     
  40.  
  41.  
  42. int main ()
  43. {
  44.    ATest aTest;
  45.    Test<ATest> aTestTemp (aTest);
  46.    
  47.    aTestTemp.run(aTest.func());
  48.    return 0;
  49. }
  50.  
  51. My first question is, should (a) work? A reading of Stroustrup doesn't
  52. make this clear, at least not to me. Second question: if it shouldn't, is
  53. there another way to do what I'm after here? Third question, for
  54. Metrowerks: if it should work, do you plan to support it soon?
  55.  
  56. -- 
  57. rs/rshapiro@bbn.com
  58.